package com.commerce.constellation.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.commerce.constellation.R;

/**
 * 自定义图片控件
 */
public class HeartView extends androidx.appcompat.widget.AppCompatImageView {

    // 画笔  抗锯齿 | 使位图过滤的位掩码标志
    private static final Paint sPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    //图片资源id
    private int mHeartResId = R.drawable.heart;
    private int mHeartBorderResId = R.drawable.heart_border;
    //图片
    private static Bitmap sHeart;
    private static Bitmap sHeartBorder;
    //画布
    private static final Canvas sCanvas = new Canvas();

    public HeartView(@NonNull Context context) {
        super(context);
    }

    public HeartView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HeartView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void setColor(int color) {
        //创建随机颜色图片
        Bitmap heart = createHeart(color);
        //将这个图片放到这个自定义控件中。
        this.setImageDrawable(new BitmapDrawable(getResources(), heart));
    }


    public Bitmap createHeart(int color) {
        if (sHeart == null) {
            sHeart = BitmapFactory.decodeResource(getResources(), mHeartResId);
        }
        if (sHeartBorder == null) {
            sHeartBorder = BitmapFactory.decodeResource(getResources(), mHeartBorderResId);
        }
        Bitmap heart = sHeart;
        Bitmap heartBorder = sHeartBorder;
        Bitmap bm = createBitmapSafely(heartBorder.getWidth(), heartBorder.getHeight());
        if (bm == null) {
            return null;
        }
        Canvas canvas = sCanvas;
        //指定画布要绘制到的位图
        canvas.setBitmap(bm);
        Paint p = sPaint;
        //绘制指定的位图，其左上角在(x，y)，使用指定的绘画，由当前矩阵转换。
        canvas.drawBitmap(heartBorder, 0, 0, p);
        //图形混合滤镜 
        PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(color
                , PorterDuff.Mode.SRC_ATOP/*丢弃不覆盖目标像素的源像素。在目标像素上绘制剩余的源像素。*/);
        //设置颜色过滤器
        p.setColorFilter(porterDuffColorFilter);

        float dx = (heartBorder.getWidth() - heart.getWidth()) / 2f;
        float dy = (heartBorder.getHeight() - heart.getHeight()) / 2f;

        canvas.drawBitmap(heart, dx, dy, p);
        //清除颜色过滤器
        p.setColorFilter(null);
        //清空画布
        canvas.setBitmap(null);
        return bm;
    }

    private static Bitmap createBitmapSafely(int width, int height) {
        try {
            //创建一个图片 宽 高 色彩模式（4*8 =32位）
            return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError error) {
            //抛出内存溢出错误
            error.printStackTrace();
        }
        return null;
    }

}


